package com.zust.data.util;

import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.zust.data.util.RegexUtil.phonePattern;

/**
 * @author senselyan
 * @date 2018/5/14 15:47
 */
public class ParamUtil {

    /**
     * 判断基本参数
     * @param str
     * @param min 最小长度(包含)
     * @param max 最大长度(包含)
     * @return
     */
    public static SimpleResponseDto checkParamStr(String str, Integer min, Integer max) {
        if (StringUtils.isEmpty(str)) {
            return new SimpleResponseDto(false, "参数为空");
        } else if (str.length() < min || str.length() < 0){
            return new SimpleResponseDto(false, "参数过短");
        } else if (str.length() > max) {
            return new SimpleResponseDto(false, "参数过长");
        } else if (checkStrAllSame(str.toCharArray())){
            return new SimpleResponseDto(false, "参数都一样");
        } else {
            return new SimpleResponseDto(true);
        }
    }

    /**
     * 1、限制输入微信、微信号、“wx”、“vx”、“qq”、“QQ号””、“手机”、“手机号”、“电话”；不区分大小写；后台用“*”代替；
     * 2、限制输入手机号码；后台用“*”代替；
     * @param str
     * @return
     */
    public static String filterContact(String str) {
        str = str.replaceAll("微信", "*");
        str = str.replaceAll("微信号", "*");
        str = str.replaceAll("(?i)wx", "*");
        str = str.replaceAll("(?i)vx", "*");
        str = str.replaceAll("(?i)qq", "*");
        str = str.replaceAll("手机", "*");
        str = str.replaceAll("手机号", "*");
        str = str.replaceAll("电话", "*");
        str = str.replaceAll("电话", "*");
        //匹配手机号
        Matcher matcher = phonePattern.matcher(str);
        return matcher.replaceAll("****");
    }

    private static boolean checkStrAllSame(char[] strs) {
        if (strs.length <= 1) {
            return true;
        }
        if (strs[strs.length-2] != strs[strs.length-1]) {
            return false;
        }
        return checkStrAllSame(Arrays.copyOf(strs, strs.length-1));
    }


    public static void main(String[] args) {
        System.out.println(filterContact("asdasdqqasdasd"));
        System.out.println(filterContact("asdasdqQasdasd"));
        System.out.println(filterContact("asdasdQQasdasd"));
        System.out.println(filterContact("asdasdQqasdaVx"));
        System.out.println(filterContact("asdasdQqasdaVx"));
        System.out.println(filterContact("2017年 我在XXX进行了一些商业保险 手机号为17826806837"));
        System.out.println(filterContact("2017年 我在XXX进行了一些商业保险 为15026806837"));


        System.out.println(checkStrAllSame("".toCharArray()));
        System.out.println(checkStrAllSame("0".toCharArray()));
        System.out.println(checkStrAllSame("s".toCharArray()));
        System.out.println(checkStrAllSame("ss".toCharArray()));
        System.out.println(checkStrAllSame("sa".toCharArray()));
        System.out.println(checkStrAllSame("sssss".toCharArray()));

    }

    static class SimpleResponseDto {
        private Boolean suc;
        private String info;

        public SimpleResponseDto(Boolean suc) {
            this.suc = suc;
        }

        public SimpleResponseDto(Boolean suc, String info) {
            this.suc = suc;
            this.info = info;
        }

        public Boolean getSuc() {
            return suc;
        }

        public void setSuc(Boolean suc) {
            this.suc = suc;
        }

        public String getInfo() {
            return info;
        }

        public void setInfo(String info) {
            this.info = info;
        }
    }

}
